package org.mineground.commands.irc;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.jibble.pircbot.Colors;
import org.jibble.pircbot.User;
import org.mineground.Main;
import org.mineground.handlers.irc.CommandExecutor;
import org.mineground.handlers.irc.UserLevel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NTotalPlayers implements CommandExecutor {
    private static final Logger ExceptionLogger = LoggerFactory.getLogger(NTotalPlayers.class);
    
    @Override
    public void onCommand(User sender, UserLevel level, String channel, String command, String args[]) {
	try {
	    PreparedStatement statement = Main.getInstance().getDatabaseHandler().getConnection().prepareStatement("SELECT COUNT(*) FROM lvm_players");
	    statement.execute();

	    ResultSet result = statement.getResultSet();
	    result.next();

	    int total = result.getInt(1);

	    StringBuilder out = new StringBuilder();
	    out.append("* There are a total of ");
	    out.append(Colors.BOLD);
	    out.append(total);
	    out.append(" players ");
	    out.append(Colors.NORMAL);
	    out.append("registered with LVM.");

	    Main.getInstance().getIRCHandler().sendMessage(channel, out.toString());
            statement.close();
	}

	catch (Exception exception) {
            ExceptionLogger.error("Exception caught", exception);
	}
    }
}